package app.labs.LabServlet;

import app.AppInfo;
import app.area.SuperArea;
import app.form.Form;
import app.labs.CheckPoint;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class Controller {
    
    private PrintWriter responsePrintWriter;
    private String resourceFolder = AppInfo.resource;
    private HttpSession session;
    
    public Controller (HttpServletRequest request, PrintWriter responsePrintWriter) {
        this.session = request.getSession();
        this.responsePrintWriter = responsePrintWriter;
    }
    
    private void out (Object obj) {
        responsePrintWriter.println (obj);
    }
    
    public void getMainPage () {
        Form form = new Form ();
        
        out("<html>");
        out("<head>");
        out("<title>Check Point in Area Servlet</title>");

        out("<link href='"+resourceFolder+"/css/style.css' rel='stylesheet' type='text/css' />");
        out("<style>");
        out(form.renderCSS());
        out("</style>");

        out("<script type='text/javascript' src='"+resourceFolder+"/javascript/lib.js'></script>");
        out("<script type='text/javascript' src='"+resourceFolder+"/javascript/components.js'></script>");
        out("<script type='text/javascript' src='"+resourceFolder+"/javascript/LabServlet.js'></script>");
        out("<script type='text/javascript'>\r\n" + form.renderJS() + "\r\n</script>");
        out("</head>");
        out("<body onLoad='JavaScript: bodyReady();'>"
                + "<h2>Лабораторная работа #1</h2>"
                + "<a href=\"/CheckPointInAreaMP/\">На главную</a><br />");

        out("<p>Выполнили: "+AppInfo.authors+"</p>"
            + "<p>Вариант: "+AppInfo.variant+"<p>"
            + "<p>Группа: "+AppInfo.group+"<p>"
            + "<div class='container'>");
        out("<div class='form'><img class='var' src='"+resourceFolder+"/images/var.png' /><div class='form_container'>");
        out(form.renderHTML());
        out("</div></div>");
        out("<div class='result'>"
                + "<p>Валидность</p>"
                + "<div class='valid'></div>"
                + "<p>Ответ от сервера</p>"
                + "<div class='response'>"
                    + "<i>Выберите точку и нажмите 'Отправить'</i>"
                + "</div>"
            + "</div>"
            + "</div>");
        out("</body>");
        out("</html>");
    }
    
    public void checkPoint (float x, float y, float r) {
        SuperArea superArea = new SuperArea();
        
        List<CheckPoint> checkPointHistory = (List<CheckPoint>)session.getAttribute("checkPointHistory");
        if (checkPointHistory == null) {
            checkPointHistory = new ArrayList<CheckPoint>();
        }
        checkPointHistory.add(new CheckPoint(x, y, r, superArea.contains(x, y, r)));
        session.setAttribute("checkPointHistory", checkPointHistory);

        StringBuilder str = new StringBuilder("");
        
        str.append("{ \"points\": [ ");
        for (int i = 0; i < checkPointHistory.size(); ++ i) {
            CheckPoint cp = checkPointHistory.get(i);
            str.append("{ \"x\": \""+cp.getX()+"\", \"y\": \""+cp.getY()+"\", \"r\": \""+cp.getR()+"\", \"contains\": \""+(cp.isContains() ? "да" : "нет")+"\" }");
            if (i < checkPointHistory.size() - 1) {
                str.append(",");
            }
        }
        str.append("]}");
        out(str);
    }
}
